class TblocksController < ApplicationController
  # GET /tblocks
  # GET /tblocks.xml
  # GET /tblocks.fxml

  def section_names
    names = Tblock::SECTION_NAMES
    respond_to do |format|
      format.json { render :json => names }
      format.fxml { render :fxml => names }
    end
  end

  def index
    @tblocks = Tblock.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tblocks }
      format.fxml  { render :fxml => @tblocks }
      format.json  { render :json => @tblocks }
    end
  end

  # GET /tblocks/1
  # GET /tblocks/1.xml
  # GET /tblocks/1.fxml
  def show
    @tblock = Tblock.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @tblock }
      format.fxml  { render :fxml => @tblock.to_fxml(:include => :nf_rect) }
      format.json  { render :json => @tblock.to_json(:include => :nf_rect) }
    end
  end

  # GET /tblocks/new
  # GET /tblocks/new.xml
  def new
    @tblock = Tblock.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @tblock }
      format.json  { render :json => @tblock }
    end
  end

  # GET /tblocks/1/edit
  def edit
    @tblock = Tblock.find(params[:id])
  end

  # POST /tblocks
  # POST /tblocks.xml
  # POST /tblocks.fxml
  def create
    @tblock = Tblock.new(params[:tblock])

    respond_to do |format|
      if @tblock.save
        flash[:notice] = 'Tblock was successfully created.'
        format.html { redirect_to(@tblock) }
        format.xml  { render :xml => @tblock, :status => :created, :location => @tblock }
        format.json  { render :json => @tblock.to_json(:include => :nf_rect) }
        format.fxml  { render :fxml => @tblock.to_fxml(:include => :nf_rect) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @tblock.errors, :status => :unprocessable_entity }
        format.json  { render :json => @tblock.errors }
        format.fxml  { render :fxml => @tblock.errors }
      end
    end
  end

  # PUT /tblocks/1
  # PUT /tblocks/1.xml
  # PUT /tblocks/1.fxml
  def update
    @tblock = Tblock.find(params[:id])
    if params[:tblock][:classification]
      @tblock.article.annotate! # switch article to annotated state
    end
    @saved = @tblock.update_attributes(params[:tblock])

    respond_to do |format|
      if @saved
        flash[:notice] = 'Tblock was successfully updated.'
        format.html { redirect_to(@tblock) }
        format.xml  { head :ok }
        format.fxml  { render :fxml => @tblock }
        format.json {  render :json => @tblock }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tblock.errors, :status => :unprocessable_entity }
        format.fxml  { render :fxml => @tblock.errors }
        format.json {  render :json => @tblock.errors }
      end
    end
  end

  # DELETE /tblocks/1
  # DELETE /tblocks/1.xml
  # DELETE /tblocks/1.fxml
  def destroy
    @tblock = Tblock.find(params[:id])
    @tblock.destroy

    respond_to do |format|
      format.html { redirect_to(tblocks_url) }
      format.xml  { head :ok }
      format.fxml  { render :fxml => @tblock }
      format.json  { render :json => @tblock }
    end
  end
end
